/**
 * 
 */
package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.member.controller.MemberUDTopListController;
import com.tianqu.tms.www.transfer.controller.TransferListController;
import com.tianqu.tms.www.transfer.viewhelper.TransferViewHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jiazheng Sun
 */
public class TransferListAction extends TmsAction {
	//
	private static final Logger LOGGER = Logger.getLogger(TransferListAction.class);
	
	public final static String ASC = "asc";
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, 
	HttpServletRequest request,HttpServletResponse response, UserContext uc) throws Exception {
		//
		LOGGER.debug(this.getClass().getName() + "#execute was called.");

		TransferViewHelper view = new TransferViewHelper();
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, view.getPageNo());
			uc.setAttribute(SORTKEY, view.getSortKey());
			uc.setAttribute(MAXINDEX, view.getMaxIndex());
			uc.setAttribute(ASC, view.getAsc());
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		TransferListController controller = new TransferListController();
		
		
		String action = request.getParameter("action");
		String searchType = request.getParameter("searchType");
		
		// search condition
		final String licensePrefix = LicensePlateConfigConstant.defLicensePlate;
		String vin = request.getParameter("vin");
		String serial = request.getParameter("serial");
		String licensePlate = StringUtils.equalsIgnoreCase(request.getParameter("licensePlate"), licensePrefix) ? "" : request.getParameter("licensePlate");
		
		controller.setVin(vin);
		controller.setSerial(serial);
		controller.setAction(action);
		controller.setSearchType(searchType);
		controller.setLicensePlate(licensePlate); controller.execute(uc);	
		view = (TransferViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		TransferAcceptedInfo transfer = (TransferAcceptedInfo)uc.getAttribute(MemberUDTopListController.PAGING);
		// viewhelp中设置翻页。
		view.setAsc(Boolean.toString(transfer.isAsc()));
		view.setDateTo(Integer.toString(transfer.getDateTo()));
		view.setPageNo(Integer.toString(transfer.getPageNo()));
		view.setSortKey(Integer.toString(transfer.getSortKey()));
		view.setDateFrom(Integer.toString(transfer.getDateFrom()));
		view.setMaxIndex(Integer.toString(transfer.getMaxIndex()));
		view.setTotalCount(Integer.toString(transfer.getTotalCount()));
		
		if (searchType != null && searchType.equals("all") && action != null && action.equals("search")) {
			if(StringUtil.isNullOrBlank(serial) &&StringUtil.isNullOrBlank(licensePlate) &&StringUtil.isNullOrBlank(vin)){
				view.setTransfers(null);
				view.setErrorCode(TmsConstants.U0040);
			}
			if(!StringUtil.isNullOrBlank(serial) &&(!StringUtil.isNullOrBlank(licensePlate) ||!StringUtil.isNullOrBlank(vin))){
				view.setTransfers(null);
				view.setErrorCode(TmsConstants.U0040);
			}
			if(StringUtil.isNullOrBlank(serial) &&(!StringUtil.isNullOrBlank(licensePlate) &&!StringUtil.isNullOrBlank(vin))){
				view.setTransfers(null);
				view.setErrorCode(TmsConstants.U0040);
			}
			
			if (StringUtil.isNullOrBlank(vin) && StringUtil.isNullOrBlank(serial) && StringUtil.isNullOrBlank(licensePlate)) {
				view.setTransfers(null);
				view.setErrorCode(TmsConstants.U0040);
				
			} 
			
			if (StringUtil.isNullOrBlank(view.getErrorCode())) {
				if (view.getTransfers() == null || view.getTransfers().size() == 0) {
					view.setTransfers(null);
					view.setErrorCode(TmsConstants.U0039);
				} else {
					final int size = view.getTransfers().size();
					if (!StringUtil.isNullOrBlank(vin) && size > 1) {
						view.setTransfers(null);
						view.setErrorCode(TmsConstants.U1054);
					}
					if (!StringUtil.isNullOrBlank(serial) && size > 1) {
						view.setTransfers(null);
						view.setErrorCode(TmsConstants.U1405);
					}
					if (!StringUtil.isNullOrBlank(licensePlate) && size > 1) {
						view.setTransfers(null);
						view.setErrorCode(TmsConstants.U1406);
					}
				}	
			}
		}
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		//
		LOGGER.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
